package JavaSE.Class14;

public class Demo {

    public static void main(String[] args) {
        String s1="abcdefg";
        String s2="     a     b  c d e      ";
        System.out.println(s1.substring(2));//从2下标开始切
        System.out.println(s1.substring(2,5));//从[2,5)下标
        System.out.println("["+s2.trim()+"]");//去掉开头和末尾的空格

    }

    //分割字符串
    public static void main8() {
        String s1="name=zs&age=10";
        //spit的返回值是数组
        String[] s=s1.split("&");
        for(int i=0;i<s.length;i++){
            System.out.println(s[i]);
        }
        System.out.println("===========");
        for(int i=0;i<s.length;i++){
            for(int j=0;j<s.length;j++){
                String[] ss=s[i].split("=");
                System.out.print(ss[j]+" ");
            }
            System.out.println();
        }
        System.out.println("============");
        String s2="192.168.1.12";
        // .不是. \\.是才是点.(转义字符）
        String[] ss=s2.split("\\.");
        for(int i=0;i<ss.length;i++){
            System.out.println(ss[i]);
        }
        //ps."|""*""+"都要加上"\\"
        //如果是"\"则要"\\\\"
        System.out.println("==============");
        //如果一个一个字符串有多个分隔符可以加"|"作连字符
        String[] sss=s1.split("=|&");
        for(int i=0;i<sss.length;i++){
            System.out.print(sss[i]+" ");
        }

    }

    //字符串替换
    public static void main7() {
        String s1="ababcabcd";
        System.out.println(s1.replace('a','1'));//把所有a换成1
        System.out.println(s1.replace("ab","1"));//把所有ab换成1
        System.out.println(s1.replaceFirst("ab","1"));//把第一个出现ab换成1
        System.out.println(s1.replaceAll("abc","1"));//把所有的ab换成1

    }


    public static void main6() {
        //字符串数组化
        String s1="hello world";
        char[] ch=s1.toCharArray();
        for(int i=0;i<ch.length;i++){
            System.out.print(ch[i]);
        }

        //格式化
        String s2=String.format("%d-%d-%d",2025,5,20);
        System.out.println(s1);
    }

    //类型转换
    public static void main5() {
        String s1="1234";
        String s2="12.34";
        int a=1234;
        double b=3.14;
        System.out.println("=========");
        System.out.println(Integer.parseInt(s1)+1);//int
        System.out.println(Double.parseDouble(s2)+1);//double
        System.out.println("=========");
        System.out.println(String.valueOf(a)+1);//String
        System.out.println(String.valueOf(b)+1);//String
        System.out.println("=========");
        String s4=String.valueOf(new Student(12,"yws"));
        System.out.println(s4);

    }

    //大小写转换
    public static void main4() {
        String s1 = "Hello World!";
        String s2 = "AbCdEf";

        //他们都是new String 并不会修改原来的字符串

        //小写
        System.out.println(s1.toLowerCase());
        System.out.println(s2.toLowerCase());
        System.out.println("============");
        //大写
        System.out.println(s1.toUpperCase());
        System.out.println(s2.toUpperCase());
    }

    //Index和lastIndex的用法
    public static void main3() {
        String s1="abababcabcd";
        for(int i=0;i<s1.length();i++){
            char ch=s1.charAt(i);
            System.out.print(ch+" ");
        }
        System.out.println();
       //返回第一次出现的下标
        System.out.println(s1.indexOf("a"));
        System.out.println(s1.indexOf("abc"));
        //从fromIndex开始找str 返回第一次的下标
        System.out.println(s1.indexOf("ab",1));
        //从后往前找 返回第一次出现str的下标
        System.out.println(s1.lastIndexOf("b"));
        //ps.lastIndex的用法和Index一样
    }

    //String类型的比较
    public static void main2() {
        String s1=new String("Hello");
        String s2=new String("Hello");
        String s3=new String("hello");
        String s4=s1;//把s1的地址给s4

        System.out.println(s1.compareTo(s2));
        System.out.println(s1.compareTo(s3));
        System.out.println(s1.compareTo(s4));
        System.out.println("==========");//看地址
        System.out.println(s1==s2);
        System.out.println(s1==s3);
        System.out.println(s1==s4);
        System.out.println("==========");//看值（看内容)
        System.out.println(s1.equals(s2));
        System.out.println(s1.equals(s3));
        System.out.println(s1.equals(s4));
        System.out.println("==========");//忽略大小写
        System.out.println(s1.compareToIgnoreCase(s2));
        System.out.println(s1.compareToIgnoreCase(s3));
        System.out.println(s1.compareToIgnoreCase(s4));
        System.out.println("==========");


    }

    //String的定义
    public static void main1() {
        String s1 = "Hello";
        String s2=new String("Hello");
        char[] ch={'a','b','c'};
        String s3=new String(ch);
        Student s=new Student(10,"zs");
        String s4=s.toString();

        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        System.out.println(s4);
    }

    static class Student{
        String name;
        int age;

        public Student(int age, String name) {
            this.age = age;
            this.name = name;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "age=" + age +
                    ", name='" + name + '\'' +
                    '}';
        }
    }
}
